<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends MX_Controller {
	
	protected $id = 0;
	
	protected $record_per_page = 0;
	
	protected $validation = array(
								array(
									'field' => 'title',
									'label' => 'lang:blog_category_title',
									'rules' => 'trim|htmlspecialchars|required|max_length[100]|callback__check_category'
								)
							);
	
    public function __construct()
    {
        parent::__construct();
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		$this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
        $this->form_validation->CI =& $this;
		
		$this->lang->load('users/user');
		$this->lang->load('admin/category');
		
		$this->template
				->set_theme('default')
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
				->set_breadcrumb('Home', 'admin/dashboard');
    }
	
	public function index()
	{
		$this->create();
	}
	
	public function create()
	{
		if (!$this->auth->is_logged_in())
		{
			redirect('users/login', 'refresh');
		}
		
		$this->load->model('Blog_categories_m', 'category');
		
		// Set the validation rules
		if (isAjax())
		{
			if($this->input->post('isAjax'))
			{
				$title = $this->input->post('title');
				if ($this->category->create_category($title))
				{
					return TRUE;
				}
			}
			else
			{
				$this->load->view('admin/category/create');
			}
			
		}
		else
		{
			//$this->form_validation->set_rules($this->validation);
			//
			//if ($this->form_validation->run())
			//{
			//	
			//	if ($this->category->create_category($title))
			//	{
			//		redirect('admin/create');
			//	}
			//}
		
			$this->template
					->set_breadcrumb('Categories','admin/lists/')
					->set_breadcrumb(' Create')
					->append_metadata($this->asset->css('modal.css'))
					->set_layout('modal')
					->build('admin/category/create');
		}
	}
}